package testes.vSys.action;

import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import testes.vSys.bean.Moviment;
import testes.vSys.dao.DAOFactory;
import testes.vSys.dao.IMovimentDao;
import testes.vSys.form.MovimentReportForm;


public class MovimentReportAction extends DispatchAction{
	
	private final String MOVIMENT_COLLECTION = "movimentCollection";
	
	public ActionForward load(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return mapping.findForward("movimentReport");
	}

public ActionForward search(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response) throws Exception {

		MovimentReportForm movimentForm = (MovimentReportForm)form;
	
		System.out.println(movimentForm.getOpr());
	
		IMovimentDao movimentDAO = DAOFactory.getMovimentDao();
		
		if((movimentForm.getDateBegin().compareTo("") != 0  ) && (movimentForm.getDateEnd().compareTo("") != 0 ))
		{
		
			String dia = movimentForm.getDateBegin().substring(0,2);
			String mes = movimentForm.getDateBegin().substring(3,5);
			String ano = movimentForm.getDateBegin().substring(6,10);
		
			String diaEnd = movimentForm.getDateEnd().substring(0,2);
			String mesEnd = movimentForm.getDateEnd().substring(3,5);
			String anoEnd = movimentForm.getDateEnd().substring(6,10);

			if( (Integer.parseInt(mes) == 1 ) || ( Integer.parseInt(mes) == 3) ||( Integer.parseInt(mes) == 5) ||
					( Integer.parseInt(mes) == 7) || ( Integer.parseInt(mes) == 8 ) || (Integer.parseInt(mes) == 10 ) ||
					( Integer.parseInt(mes) == 12 ) )
			{
				if((Integer.parseInt(dia) > 31))
				{
					request.setAttribute("dataInvalida", true);
					return load(mapping, form, request, response);
				}
			}
			else if( (Integer.parseInt(mes) == 4) || ( Integer.parseInt(mes) == 6) ||( Integer.parseInt(mes) == 9) ||
					( Integer.parseInt(mes) == 11) )
			{
				if(Integer.parseInt(dia) > 30)
				{
					request.setAttribute("dataInvalida", true);
					return load(mapping, form, request, response);
				}
			}
			else if( Integer.parseInt(mes) == 2)
			{
				if(Integer.parseInt(dia) > 29)
				{
					if(!new GregorianCalendar().isLeapYear(Integer.parseInt(ano)))
					{
						request.setAttribute("dataInvalida", true);
						return load(mapping, form, request, response);
					}
					
				}
				else if(Integer.parseInt(dia) >= 30)
				{
					request.setAttribute("dataInvalida", true);
					return load(mapping, form, request, response);
				}
			}
			
			if( (Integer.parseInt(mesEnd) == 1 ) || ( Integer.parseInt(mesEnd) == 3) ||( Integer.parseInt(mesEnd) == 5) ||
					( Integer.parseInt(mesEnd) == 7) || ( Integer.parseInt(mesEnd) == 8 ) || (Integer.parseInt(mesEnd) == 10 ) ||
					( Integer.parseInt(mesEnd) == 12 ) )
			{
				if((Integer.parseInt(diaEnd) > 31))
				{
					request.setAttribute("dataInvalida", true);
					return load(mapping, form, request, response);
				}
			}
			else if( (Integer.parseInt(mesEnd) == 4) || ( Integer.parseInt(mesEnd) == 6) ||( Integer.parseInt(mesEnd) == 9) ||
					( Integer.parseInt(mesEnd) == 11) )
			{
				if(Integer.parseInt(diaEnd) > 30)
				{
					request.setAttribute("dataInvalida", true);
					return load(mapping, form, request, response);
				}
			}
			else if( Integer.parseInt(mesEnd) == 2)
			{
				if(Integer.parseInt(diaEnd) == 29)
				{
					if(!new GregorianCalendar().isLeapYear(Integer.parseInt(ano)))
					{
						request.setAttribute("dataInvalida", true);
						return load(mapping, form, request, response);
					}
					
				}
				else if(Integer.parseInt(diaEnd) >= 30)
				{
					request.setAttribute("dataInvalida", true);
					return load(mapping, form, request, response);
				}
			}
							
			
			Date dateBegin = new GregorianCalendar(Integer.parseInt(ano), Integer.parseInt(mes),Integer.parseInt(dia)).getTime();
			Date dateEnd = new GregorianCalendar(Integer.parseInt(anoEnd), Integer.parseInt(mesEnd),Integer.parseInt(diaEnd)).getTime();
						
					
			if(dateBegin.compareTo(dateEnd) < 0 )
			{
				Collection<Moviment> moviment = movimentDAO.getMovimentBetween(dateBegin, dateEnd);
				if(moviment.isEmpty() == false)
				{
					request.setAttribute(MOVIMENT_COLLECTION, moviment);
					request.setAttribute("show", true);
				}
				else
				{
					request.setAttribute("error", true);
				}
			}
		}
		else
			request.setAttribute("campoVazio", true);
			
	return load(mapping, form, request, response);
 
	}
}
